C/C++

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C/C++ >

C++_C++编写生成不重复的随机数代码,C++编写生成不重复的随机数代

来源:网络整理  作者:  发布时间:2020-12-19 18:13
C++编写生成不重复的随机数代码,C++编写生成不重复的随机数代码vectorint getRandom(int total){ srand((int)time(NULL)); std::vecto...

再来一例:

#include<stdio.h> #include <time.h> #include "iostream" #include <math.h> #define N 53 using namespace std; //print array void display(int *a) { for (int i =0;i<N;i++) { cout<<" "<<a[i]<<" "; } } int main(void) { int b[N],a[N]; for (int i =0;i<N;i++) { b[i] = i+1; } // random(a); srand((unsigned)time(NULL)); int MaxIndex = N; for ( i= 0;i<N;i++) { // int index = (int)rand()%MaxIndex;//随机一个 0 - 52的index a[i] = b[index]; //随机到的数字给a[i],i from 0 to N-1 b[index] = b[MaxIndex-1]; MaxIndex--; } display(a); return 0; }

C++编写生成不重复的随机数代码

vector<int> getRandom(int total) { srand((int)time(NULL)); std::vector<int> input = *new std::vector<int>(); for (int i = 0; i < total; i++) { input.push_back(i); } vector<int> output = *new vector<int>(); int end = total; for (int i = 0; i < total; i++) { vector<int>::iterator iter = input.begin(); int num = random()%end; iter = iter+num; output.push_back(*iter); input.erase(iter); end--; } return output; }

void permutation(int n, int *z_array) { int i, j, k, z; int buffer[N]; /* 初始化数组 */ for (i=0; i<n; i++) buffer[i]=0; /* 准备生成随机数,以当前时间为种子 */ srand((unsigned)time((long *)0)); /* 获得不重复的随机数据 */ for (i=0; i<n; i++) { /* 获得0~(n-i)的随机数据 */ z = rand()%(n-i); j=0; k=0; while (j<=z) { if (buffer[j+k]==0) j++; else k++; } buffer[j+k-1]=1; z_array[i]=j+k-1; } return; }

方法三:来个复杂点的

以上3种方法均可实现生成不重复的随机数,具体的效率如何,小伙伴们自己测试下吧。

相关热词: C++

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/cjj/5710.shtml

最新文章
只需要在调用Ctrl+B编译后 只需要在调用Ctrl+B编译后

时间:2021-01-13

OpenGL超级宝典visual studio OpenGL超级宝典visual studio

时间:2021-01-04

Directx11 教程(2) 基本的wi Directx11 教程(2) 基本的wi

时间:2021-01-04

LeetCode11ContainerWithMostWate LeetCode11ContainerWithMostWate

时间:2021-01-04

C语言简单IT之家速成 C语言简单IT之家速成

时间:2020-12-27

三分钟了解Activity工作流 三分钟了解Activity工作流

时间:2020-12-27

编译器是如何实现32位整型 编译器是如何实现32位整型

时间:2020-12-27

C++中lower_bound函数和upper C++中lower_bound函数和upper

时间:2020-12-27

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C++_C++编写生成不重复的随机数代码,C++编写生成不重复的随机数代

2020-12-19 编辑:

再来一例:

#include<stdio.h> #include <time.h> #include "iostream" #include <math.h> #define N 53 using namespace std; //print array void display(int *a) { for (int i =0;i<N;i++) { cout<<" "<<a[i]<<" "; } } int main(void) { int b[N],a[N]; for (int i =0;i<N;i++) { b[i] = i+1; } // random(a); srand((unsigned)time(NULL)); int MaxIndex = N; for ( i= 0;i<N;i++) { // int index = (int)rand()%MaxIndex;//随机一个 0 - 52的index a[i] = b[index]; //随机到的数字给a[i],i from 0 to N-1 b[index] = b[MaxIndex-1]; MaxIndex--; } display(a); return 0; }

C++编写生成不重复的随机数代码

vector<int> getRandom(int total) { srand((int)time(NULL)); std::vector<int> input = *new std::vector<int>(); for (int i = 0; i < total; i++) { input.push_back(i); } vector<int> output = *new vector<int>(); int end = total; for (int i = 0; i < total; i++) { vector<int>::iterator iter = input.begin(); int num = random()%end; iter = iter+num; output.push_back(*iter); input.erase(iter); end--; } return output; }

void permutation(int n, int *z_array) { int i, j, k, z; int buffer[N]; /* 初始化数组 */ for (i=0; i<n; i++) buffer[i]=0; /* 准备生成随机数,以当前时间为种子 */ srand((unsigned)time((long *)0)); /* 获得不重复的随机数据 */ for (i=0; i<n; i++) { /* 获得0~(n-i)的随机数据 */ z = rand()%(n-i); j=0; k=0; while (j<=z) { if (buffer[j+k]==0) j++; else k++; } buffer[j+k-1]=1; z_array[i]=j+k-1; } return; }

方法三:来个复杂点的

以上3种方法均可实现生成不重复的随机数,具体的效率如何,小伙伴们自己测试下吧。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/cjj/5710.shtml

相关文章

风云图片

推荐阅读

返回C/C++频道首页